<?php

namespace App\Http\Controllers\Home;

use App\Model\Lesson;
use App\Model\Video;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
//    个人视频
    public function index()
    {
        $lessons=Lesson::get();
      return view('home.lesson.index',compact('lessons'));
    }
    //全部课程
    public function lists()
    {
        $lessons=Lesson::paginate(6);
        return view('home.lesson.lists',compact('lessons'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $field = [
            'lesson'=>[
                'title'=>'','description'=>'','icon'=>asset ('org/hdjs/image/nopic.jpg'),
            ],
            'video'=>[],
        ];
      return view('home.lesson.create',compact('field'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)

    {
        $data = json_decode ($request->field,true);
        $this->validateData ($data['lesson']);
        $lesson = auth ()->user ()->lesson()->create($data['lesson']);
        $lesson->video()->createMany($data['video']);
        //成功提示
        return redirect ()->route ('home.lesson.index')->with ('success','添加成功');
    }
    public function validateData($data){
        //https://laravel-china.org/docs/laravel/5.6/validation/1372
        //自动重定向
        \Validator::make($data,[
            'title'=>'required',
            'description'=>'required',
            'icon'=>'required',
        ],[
            'title.required'=>'请输入课程标题',
            'description.required'=>'请输入课程描述',
            '.required'=>'请上传缩略图',
        ])->validate ();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function show(Lesson $lesson,Request $request)
    {

       if(isset($request['lesson_id'])){
           $lesson_id=$request->lesson_id;
           $video_id=$request->video_id;
       }else{
           $lesson_id=$lesson->id;
           $video_id=0;
       }
        $videos=Video::where('lesson_id',$lesson_id)->get();
       if ($video_id){
           $url=Video::where('id',$video_id)->first()->url;
       }else{
           $url='http://vjs.zencdn.net/v/oceans.mp4';
       }
            return view('home.lesson.show',compact('videos','lesson','url'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function edit(Lesson $lesson)
    {
        $field = [
            'lesson'=>$lesson->toArray (),
            'video'=>$lesson->video->toArray(),
        ];
        //dd($field);
        return view ('home.lesson.edit',compact ('field'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Lesson $lesson)
    {
        $data = json_decode ($request->field,true);
        $this->validateData ($data['lesson']);
        $lesson->update ($data['lesson']);
        $lesson->video ()->delete ();
        foreach($data['video'] as $video) {
            $lesson->video()->withTrashed()->updateOrCreate ( [ 'id' => $video['id']??0],$video )->restore();
        }
        view('home.lesson.edit');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function destroy(Lesson $lesson)
    {
        $lesson->delete ();
        $lesson->video ()->forceDelete ();
        return back ()->with ('success','删除成功');
    }
}
